package com.qianfeng.controller;

import com.qianfeng.dto.R;
import com.qianfeng.log.annotations.LogAnnotation;
import com.qianfeng.pojo.SysDevice;
import com.qianfeng.service.DeviceService;
import com.qianfeng.valigroup.Insert;
import com.qianfeng.valigroup.Select;
import com.qianfeng.valigroup.UpDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/8 15:28
 */
@RestController
@RequestMapping("/devices")
@Api(tags = "设备管理模块")
public class DeviceController {
    private DeviceService deviceService;
    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }
    @PostMapping("/device")
    @ApiOperation("添加设备")
    @LogAnnotation(title = "添加设备",business_type = 1)
    public R addDevice(@Validated(Insert.class) @RequestBody SysDevice device){
        deviceService.addDevice(device);
        return R.OK();
    }

    @GetMapping("/device/{deviceId}")
    @ApiOperation("根据id查询设备")
    @LogAnnotation(title = "查询设备",business_type = 0)
    public R findByDeviceId(@PathVariable String deviceId){
        SysDevice sysDevice=deviceService.findByDeviceId(deviceId);
        return R.OK(sysDevice);
    }
    @GetMapping("/device")
    @ApiOperation("查找所有设备")
    @LogAnnotation(title = "查询所有设备",business_type = 0)
    public R findAll(){
//        只能查到当前用户的
        List<SysDevice> sysDevice=deviceService.findAll();
        return R.OK(sysDevice);
    }
    @DeleteMapping("/device/{deviceId}")
    @ApiOperation("删除设备")
    @LogAnnotation(title = "删除设备",business_type = 3)
    public R deleteByDeviceId(@PathVariable String deviceId){
        deviceService.deleteByDeviceId(deviceId);
        return R.OK();
    }
    @PutMapping("/device")
    @ApiOperation("修改设备")
    @LogAnnotation(title = "修改设备",business_type = 2)
    public R updateDevice(@Validated(UpDate.class) @RequestBody SysDevice device){
        deviceService.updateDevice(device);
        return R.OK();
    }
    @PostMapping("/command/{deviceId}/{command}")
    @ApiOperation("向设备发送指令")
    public R sendCommand(@PathVariable("deviceId") String deviceId,@PathVariable("command") String command){
        deviceService.sendCommand(deviceId,command);
        return R.OK();
    }


}
